<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\AddressNew as AddressModel;
use app\common\model\DeliveryArea;
use app\common\model\DeliveryArayacak;
use think\Request;

class Address extends Fornt{

	public function index(){
	    parent::wechatJs();
		set_time_limit(0);
		//删除cookie
		// cookie('poiname', null);
		// cookie('poiaddress', null);
		// cookie('latlng', null);
		//添加完地址 默认跳转的地址
		if(cookie('address_type') == 1){
			$defaultsBackUrl = url('index/cart/placepinkage');
		}else{


			if(cookie('addressbackurl')){
				$defaultsBackUrl = cookie('addressbackurl');
			}else{
				$defaultsBackUrl = url('index/cart/index');
			}
		}
		$backurl =  input('backurl') ? input('backurl') : $defaultsBackUrl;
		//parent::allAdresSetFreight();

		$map = [
			'user_id'=>USER_ID,
			'type'=> cookie('address_type') ? cookie('address_type') : 0
		];

    $list = AddressModel::where($map)->order('defaults desc')->select();
    $this->assign('list', $list);
    $this->assign('backurl', $backurl);
    return $this->fetch();
	}
	
	public function testmap(){
	
	
	    return $this->fetch();
	}
	
	
  public function add($id = null){
      parent::wechatJs();
    if (IS_POST) {
						$map = [
							'user_id'=>USER_ID,
							'type'=> cookie('address_type') ? cookie('address_type') : 0
						];
						AddressModel::where($map)->update(['defaults' => 0]);
						$data = input('post.');
						$data['defaults'] = 1;

						$result = model('AddressNew')->changes($data);

      if($result){
				//读取地址

				$address = db('AddressNew')->where(['user_id'=>USER_ID,'defaults'=>1, 'type' => 0])->find();
				//更新距离
				$place = db('place')->where(['id'=>cookie('place_id')])->find();
				$distance =  getMapdistance($place['latlng'] ,$address['latlng']);
<<<<<<< HEAD
				db('AddressNew')->where('id', $address['id'])->update(['disparity'=>$distance]);
				if(cookie('addressbackurl')){
					$backUrl = cookie('addressbackurl');
				}
=======
				db('AddressNew')->where('id', $address['id'])->update(['disparity'=>$distance]);
				if(cookie('addressbackurl')){
					$backUrl = cookie('addressbackurl');
				}
>>>>>>> 37581d8edb7a0b22980c59b2b5580e0a44b60b5e
        $this->success('添加成功！', $backUrl);
      }else{
        $this->error('添加失败！');
      }
    }else{
      if($id){
        $info = AddressModel::get($id);
        $this->assign('info', $info);
      }

      return $this->fetch();
    }
  }

  
  //address_level 区域分类  观山湖区1类  白云 云岩 南明 乌当 2类  其他3类
  public function addwxopen($id = null){
      parent::wechatJs();
      if (IS_POST) {
          $map = [
              'user_id'=>USER_ID,
              'type'=> cookie('address_type') ? cookie('address_type') : 0
          ];
          AddressModel::where($map)->update(['defaults' => 0]);
          $data = input('post.');
          $data['defaults'] = 1;  
          if (strpos($data['address'],'观山湖区'))
          {
              $data['address_level'] = 1;
          }elseif (strpos($data['address'],'白云区')||strpos($data['address'],'云岩区')||strpos($data['address'],'南明区')||strpos($data['address'],'乌当区'))
          {
              $data['address_level'] = 2;
          }
          else {
              $data['address_level'] = 3;
          }
          
          $result = model('AddressNew')->changes($data);  
          if($result){
              //读取地址
  
//               $address = db('AddressNew')->where(['user_id'=>USER_ID,'defaults'=>1, 'type' => 0])->find();
//               //更新距离
//               $place = db('place')->where(['id'=>cookie('place_id')])->find();
//               $distance =  getMapdistance($place['latlng'] ,$address['latlng']);
//               db('AddressNew')->where('id', $address['id'])->update(['disparity'=>$distance]);
    
              if(cookie('addressbackurl')){
  
                  $backUrl = cookie('addressbackurl');
  
              }  
  
              $this->success('添加成功！', $backUrl);
          }else{
              $this->error('添加失败！');
          }
      }else{
          if($id){
              $info = AddressModel::get($id);
              $this->assign('info', $info);
          }
  
          return $this->fetch();
      }
  }
  
  
  
	public function setdefaults($id = null){
		if (!$id) {
			return $this->error("非法操作！");
		}
		$info = AddressModel::find($id);
		$info->defaults = 1;
		$map = [
			'user_id'=>USER_ID,
			'type'=> cookie('address_type') ? cookie('address_type') : 0
		];
		AddressModel::where($map)->update(['defaults' => 0]);
		$result=	$info->save();
		if ($result) {
				return $this->success("设置成功！");
		} else {
				return $this->error("设置失败！");
		}
	}

	public function setqiyeDefaults($id = null, $cid){
		if (!$id) {
			return $this->error("非法操作！");
		}
		$map = ['uid'=>USER_ID,'company_id'=>$cid];
		model('Qiyeuser')->where($map)->update(['defaults' => 0]);
		model('Qiyeuser')->where('id', $id)->update(['defaults' => 1]);
		if ($result) {
				return $this->success("设置成功！");
		} else {
				return $this->error("设置失败！");
		}
	}

	public function getArayacak(){
		$list = DeliveryArayacak::order('id desc')->select();
		if(cookie('?zt_address')){
			foreach ($list as $key => $value) {
				if($value->id ==  cookie('zt_address')){
					$list[$key]['default'] = 1;
				}
			}
		}
		return json($list);
	}

	public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = AddressModel::where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}
	public function delqiye() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = model('Qiyeuser')->where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}

	public function map(){
		return $this->fetch();
	}


	public function qiyeindex($cid = null){
		$info = parent::setQiyeCommon();
		$qmap = [
			'uid'=>USER_ID,
			'company_id'=>$cid
		];
		$list = model('Qiyeuser')->where($qmap)->select();
		$this->assign('list', $list);
		return $this->fetch();
	}


	public function addqiye($cid = null, $id = null){

		$info = parent::setQiyeCommon();
		if($this->request->isPost()){
				//设置所有地址为不默认
				$map = ['uid'=>USER_ID,'company_id'=>$cid];
				model('Qiyeuser')->where($map)->update(['defaults' => 0]);
			 //添加用户信息
			 $data = input('post.');
			 $data['defaults'] = 1;
			 $data['uid'] = USER_ID;
			 $data['company_id'] = $cid;
			 $result = model('Qiyeuser')->changes($data);

				return $this->success("更新成功！");

		}else{
			if($id){
				$info = model('Qiyeuser')->find($id);
				$this->assign('info', $info);
			}
			return $this->fetch();
		}

	}

}
